/**
 * @desc
 * include:
 *      production: collect, share, play-count, like and so on
 * @date 2018-12-11 9:23:23
 *
 */

/**
 * play count
 * @param target
 */
function playCount(target) {
    $.ajax({
        type: 'post',
        url: '/production/api/count/' + target,
        dataType: 'json',
        beforeSend: function (xhr) {
            xhr.setRequestHeader($("meta[name='_csrf_header']").attr("content"), $("meta[name='_csrf']").attr("content"));
        },
        success: function (result) {
            
        }
    })
}

/**
 * collect
 * @param target
 * @param type
 */
function bindCollectEvent(ele) {
    setTimeout(function() {
        $(ele || '.operations-collect').click(function () {
            hasLogin();

            var _self = this;
            // 收藏目标类型
            var type = $(this).attr('data-collect-type');
            // id
            var id = $(this).attr('data-collect-id');
            // 操作类型
            var operation = $(this).attr('data-collect-operation');
            // 数量
            var num = parseInt($(this).siblings().text());
            $.ajax({
                type: 'post',
                url: '/personal/collect/' + type + '/' + operation + '/api/collect',
                data: {
                    dataId: id
                },
                async: false,
                dataType: 'json',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader($("meta[name='_csrf_header']").attr("content"), $("meta[name='_csrf']").attr("content"));
                },
                success: function (result) {
                    if (result.status == Messages.HTTP_STATUS.SUCCESS) {
                        if (result.data == true) {
                            num++;
                            $(_self).siblings().text(num);
                            $(_self).parent('dl,div').addClass('on');
                        } else {
                            num--;
                            $(_self).siblings().text(num);
                            $(_self).parent('dl,div').removeClass('on');
                        }
                    } else {
                        alert(result.error);
                    }
                },
                error: function (e) {
                    alert(Messages.ERROR);
                }
            })
        })
    }, 1000)
}

$(function () {

    bindCollectEvent();
})